package com.alibaba.citrus.ots.platform.enhance.repository.impl;

import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.platform.enhance.repository.ActivityQueryRepository;
import com.alibaba.citrus.ots.platform.enhance.repository.model.diff.activity.QueryActivityTermParam;
import com.alibaba.citrus.ots.platform.enhance.repository.model.diff.activity.QueryActivityUseRecordParam;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsforecastsales.activityuserecordrelation.dto.QueryActivityUseRecordByGmtModifyAscRequest;
import com.epoch.app.otsforecastsales.activityuserecordrelation.dto.QueryListByForecastSubOrderIdsRequest;
import com.epoch.app.otsforecastsales.activityuserecordrelation.model.ActivityUseRecordRelation;
import com.epoch.app.otsforecastsales.activityuserecordrelation.service.ActivityUseRecordRelationService;
import com.epoch.app.otsforecastsales.domian.activityterm.dto.QueryActivityIdsByActivityTermIdsRequest;
import com.epoch.app.otsforecastsales.domian.activityterm.dto.QueryTermSDOListByTermIdRequest;
import com.epoch.app.otsforecastsales.domian.activityterm.model.ActivityTerm;
import com.epoch.app.otsforecastsales.domian.activityterm.service.ActivityTermService;
import com.epoch.app.otsforecastsales.model.dto.ActivityTermLoadListVO;
import com.epoch.app.otsplatformenhance.model.dto.ActivityDiffCheckRequest;
import com.epoch.app.otspricecenter.sdo.ActivityTermSDO;
import com.epoch.app.otspricecenter.sdo.ActivityUseRecordRelationSDO;
import com.epoch.app.otsforecastsales.domian.activitytitle.dto.FilterActivityIdsByFacadeCodeRequest;
import com.epoch.app.otsforecastsales.domian.activitytitle.dto.FilterActivityIdsByFacadeCodeResponse;
import com.epoch.app.otsforecastsales.domian.activitytitle.service.ActivityTitleService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: qiaojiange
 * @create: 2022/01/18
 * @Description 活动查询资源层
 */
@Repository
public class ActivityDiffQueryForDiffRepositoryImpl implements ActivityQueryRepository {

    /**
     * 活动使用记录关系 service
     */
    @Autowired
    private ActivityUseRecordRelationService activityUseRecordRelationService;

    /**
     * 活动抬头 service
     */
    @Autowired
    private ActivityTitleService activityTitleService;

    /**
     * 活动 term service
     */
    @Autowired
    private ActivityTermService activityTermService;



    @Override
    @RepositoryInvoker
    public Result<List<ActivityUseRecordRelationSDO>> queryActivityUseRecord(QueryActivityUseRecordParam useRecordParam) {
        List<ActivityUseRecordRelationSDO> dataList = new ArrayList<>();
        if (useRecordParam == null) {
            return Result.success(dataList);
        }

        QueryActivityUseRecordByGmtModifyAscRequest request = new QueryActivityUseRecordByGmtModifyAscRequest();
        BeanUtils.copyProperties(useRecordParam, request);
        Result<List<ActivityUseRecordRelation>> listResult = activityUseRecordRelationService.queryActivityUseRecordByGmtModifyAsc(request);

        if (Objects.nonNull(listResult) && CollectionUtils.isNotEmpty(listResult.getResult())) {
            for (ActivityUseRecordRelation activityUseRecordRelation : listResult.getResult()) {
                ActivityUseRecordRelationSDO dto = new ActivityUseRecordRelationSDO();
                BeanUtils.copyProperties(activityUseRecordRelation, dto);
                dto.setItemQuantity(activityUseRecordRelation.getItemQuantity().intValue());
                dataList.add(dto);
            }
        }
        Result<List<ActivityUseRecordRelationSDO>> result = Result.success(dataList);
        result.setTotal(listResult.getTotal());
        return result;
    }

    @Override
    @RepositoryInvoker
    public Result<List<ActivityUseRecordRelationSDO>> queryListByForecastSubOrderIds(QueryActivityUseRecordParam queryActivityUseRecordParam) {
        Result<List<ActivityUseRecordRelationSDO>> result = Result.success(new ArrayList<>());

        QueryListByForecastSubOrderIdsRequest request = new QueryListByForecastSubOrderIdsRequest();
        request.setStart(queryActivityUseRecordParam.getStart());
        request.setLimit(1);
        request.setSubOrderIds(queryActivityUseRecordParam.getSubOrderIds());
        request.setStatus(queryActivityUseRecordParam.getStatus());
        Result<List<ActivityUseRecordRelation>> listResult = activityUseRecordRelationService.queryListByForecastSubOrderIds(request);
        if(Objects.isNull(listResult) || CollectionUtils.isEmpty(listResult.getResult())){
            return result;
        }

        request.setLimit(listResult.getTotal());
        listResult = activityUseRecordRelationService.queryListByForecastSubOrderIds(request);

        result.setTotal(listResult.getTotal());
        result.setResult(
                listResult.getResult().stream().map(e ->
                        {

                            ActivityUseRecordRelationSDO sdo = new ActivityUseRecordRelationSDO();
                            BeanUtils.copyProperties(e, sdo);
                            sdo.setItemQuantity(e.getItemQuantity() == null
                                    ? null : Integer.parseInt(e.getItemQuantity().toString()));
                            return sdo;
                        }
                ).collect(Collectors.toList()));


        return result;
    }
    /**
     * 查询更新的活动的列表数据
     * @param request 查询活动 term 列表请求
     * @return 活动 termSDO 列表
     */
    @RepositoryInvoker
    @Override
    public Result<List<ActivityTermSDO>> queryModifiedActivityTermList(ActivityDiffCheckRequest request) {
        com.epoch.app.otsforecastsales.model.dto.ActivityDiffCheckRequest checkRequest = new com.epoch.app.otsforecastsales.model.dto.ActivityDiffCheckRequest();
        BeanUtils.copyProperties(request, checkRequest);
        Result<List<ActivityTerm>> listResult = activityTermService.queryModifiedActivityTermList(checkRequest);
        List<ActivityTermSDO> activityTermSDOList = new ArrayList<>();
        if (listResult != null && CollectionUtils.isNotEmpty(listResult.getResult())) {
            for (ActivityTerm activityTerm : listResult.getResult()) {
                ActivityTermSDO sdo = new ActivityTermSDO();
                BeanUtils.copyProperties(activityTerm, sdo);
                activityTermSDOList.add(sdo);
            }
        }
        Result<List<ActivityTermSDO>> result = Result.success(activityTermSDOList);
        result.setTotal(listResult.getTotal());
        return result;
    }

    @Override
    @RepositoryInvoker
    public Result<List<String>> filterActivityIdsByActivityFacadeCode(QueryActivityUseRecordParam queryActivityUseRecordDTO) {
        FilterActivityIdsByFacadeCodeRequest request = new FilterActivityIdsByFacadeCodeRequest();
        request.setActivityIds(queryActivityUseRecordDTO.getActivityIds());
        request.setActivityType(queryActivityUseRecordDTO.getActivityType());
        FilterActivityIdsByFacadeCodeResponse response = activityTitleService.filterActivityIdsByFacadeCode(request);

        List activityCodes = response.getActivityCodes();
        return Result.success(activityCodes);
    }
    /**
     * 根据活动 termId 查询活动使用记录列表
     * @param param 查询活动使用记录参数
     * @return 活动使用列表
     */
    @RepositoryInvoker
    @Override
    public Result<List<ActivityUseRecordRelationSDO>> queryActivityUseRecordListByActivityTermId(QueryActivityUseRecordParam param) {
        com.epoch.app.otsforecastsales.model.dto.QueryActivityUseRecordParam param1 = new com.epoch.app.otsforecastsales.model.dto.QueryActivityUseRecordParam();
        BeanUtils.copyProperties(param, param1);
        Result<List<ActivityUseRecordRelation>> listResult = activityUseRecordRelationService.queryActivityUseRecordListByActivityTermId(param1);

        List<ActivityUseRecordRelationSDO> result = new ArrayList<>();
        if (Objects.nonNull(listResult) && CollectionUtils.isNotEmpty(listResult.getResult())) {
            for (ActivityUseRecordRelation activityUseRecordRelation : listResult.getResult()) {
                ActivityUseRecordRelationSDO sdo = new ActivityUseRecordRelationSDO();
                BeanUtils.copyProperties(activityUseRecordRelation, sdo);
                sdo.setItemQuantity(activityUseRecordRelation.getItemQuantity().intValue());
                result.add(sdo);
            }
        }

        Result<List<ActivityUseRecordRelationSDO>> successResult = Result.success(result);
        successResult.setTotal(listResult.getTotal());
        return successResult;
    }

    /**
     * 查询活动明细
     * @param param
     * @return
     */
    @Override
    public Result<ActivityTermSDO> queryActivityTerm(QueryActivityTermParam param) {
        QueryTermSDOListByTermIdRequest request = new QueryTermSDOListByTermIdRequest();
        request.setActivityId(param.getActivityId());
        request.setTermId(param.getActivityTermId());
        ActivityTermLoadListVO activityTermLoadListVO = activityTermService.queryTermSDOListByTermId(request);
        if (Objects.isNull(activityTermLoadListVO) || Objects.isNull(activityTermLoadListVO.getId())) {
            return null;
        }
        ActivityTermSDO activityTermSDO = new ActivityTermSDO();
        BeanUtils.copyProperties(activityTermLoadListVO, activityTermSDO);
        return Result.success(activityTermSDO);
    }


    /**
     * 根据活动项目号查询活动号
     * @param activityTermIds
     * @return
     */
    @Override
    public List<ActivityTerm> queryActivityIdsByActivityTermIds(Set<String> activityTermIds) {
        QueryActivityIdsByActivityTermIdsRequest request = new QueryActivityIdsByActivityTermIdsRequest();
        request.setActivityTermIds(new ArrayList(activityTermIds));

        Result<List<ActivityTerm>> listResult = activityTermService.queryActivityIdsByActivityTermIds(request);
        if(Objects.isNull(listResult) || CollectionUtils.isEmpty(listResult.getResult())){
            return new ArrayList<>();
        }
        return listResult.getResult();
    }
}
